import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:provider/provider.dart';
import 'package:schedule_self_client/common/api_common.dart';
import 'package:schedule_self_client/domain/user_info_manager.dart';
import 'package:schedule_self_client/init.dart';
import 'package:schedule_self_client/state_management/providers/login_status.dart';
import 'package:schedule_self_client/ui/routes.dart';

void main() async {
  await initNotifications();
  await requestPermissions();
  UserInfo userInfo = await UserInfo.initWithCache();

  final PackageInfo packageInfo = await PackageInfo.fromPlatform();
  print(packageInfo.version);

  runApp(MultiProvider(
    providers: [
      ChangeNotifierProvider(create: (_) {
        return LoginInfo(userInfo);
      }),
    ],
    child: const MyApp(),
  ));
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    // 初始化dio客户端
    apiInit(ApiCommon.getInterceptorsWrapper(context));

    MaterialApp materialApp = MaterialApp.router(
      routerConfig: router,
      debugShowCheckedModeBanner: false,
      localizationsDelegates: const [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      builder: BotToastInit(),
      supportedLocales: const [
        Locale('zh'),
        Locale('en'),
      ],
    );
    materialApp.navigatorObservers?.add(BotToastNavigatorObserver());
    return materialApp;
  }
}
